package com.waitingresult.thread.demo;

import java.util.ArrayList;
import java.util.List;

/**
 * All right reserved,Designed By WangShuai
 *
 * @author : WangShuai
 * @version : V1.0
 * @ClassName : ThredeDemo2
 * @Description :
 * @date : 2022/9/19 9:54
 */
public class ThredeDemo2 {


    public static void main(String[] args) throws Exception {
        Base base = new Base();
        M m1 = new M(base);
        M m2 = new M(base);
        M m3 = new M(base);
        M[] m = base.getM();
        m[0] = m1;
        m[1] = m2;
        m[2] = m3;
        Thread thread = new Thread(base);
        thread.start();
        System.err.println("main start end");
        List list = base.getList();
        thread.join();
        System.err.println("base list ");
        System.out.println(list.size());
    }
}
class Base implements Runnable{
    private static M[] m = new M[10];
    private volatile static List<String>  list  = new ArrayList();

    @Override
    public void run() {
        for (M m1 : m) {
            new Thread(m1).start();
        }
        System.err.println("base start end ");
    }

    public  M[] getM() {
        return m;
    }

    public  List getList() {
        return list;
    }
}
class M implements Runnable{
    private Base base;

    public M(Base base) {
        this.base = base;
    }

    @Override
    public void run() {
        System.err.println("M start");

        String threadName = Thread.currentThread().getName();
        Object o = new Object();
        synchronized (o){
            List list = base.getList();
            for (int i = 0; i < 10; i++) {
                list.add(threadName+"     ----"+i);
            }
        }
        System.err.println("M end");
    }
}
